package com.romotc.qqrobot.webqq.bean;

import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.romotc.json.tools.Jackson;
import com.romotc.qqrobot.bean.PollResult;
import com.romotc.scache.repair;

/**
 * http://d.web2.qq.com/channel/poll2 请求返回信息result value 内容对应Bean
 * @author Administrator
 * "result":
 * 	[
 * 		{
 * 			"poll_type":"message",
 * 			"value":
 * 				{"msg_id":11028,
 * 				 "from_uin":1670433408,
 * 				 "to_uin":1586030808,
 *   			 "msg_id2":589200,
 *   			 "msg_type":9,
 *      		 "reply_ip":176752342,
 *      		 "time":1326529150,
 *      		 "content":[
 *      						["font",
 *      							{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}
 *      						],"ddd "
 *      				   ]
 *      		}
 *      }
 *  ]
 */

/**
 * "value": {"msg_id":11028, "from_uin":1670433408, "to_uin":1586030808,
 * "msg_id2":589200, "msg_type":9, "reply_ip":176752342, "time":1326529150,
 * "content":[["font",{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}],"ddd "] }
 */
public class Poll2_ResponseBody_Result_Value extends HashMap {

	private long msg_id;
	private long from_uin;
	private long to_uin;
	private long msg_id2;
	private int msg_type;
	private long reply_ip;
	private long time;
	private String contentMsg;
	private ContentFont font;
	private String mode;
	private int type;
	private String name;
	private long session_id;
	private long inet_ip;
	private int cancel_type;
	
	private int lc_id;
	
	//{"retcode":0,"result":[{"poll_type":"buddies_status_change",
		//"value":{"uin":3929551529,"status":"busy","client_type":1}}]}
	private long uin;
	private String status;
	private int client_type;

	/**
	 * 通过Json串解析对象
	 * 
	 * @param msgStr
	 * @return
	 */

	/**
	 * "value": {"msg_id":11028, "from_uin":1670433408, "to_uin":1586030808,
	 * "msg_id2":589200, "msg_type":9, "reply_ip":176752342, "time":1326529150,
	 * "content":[ ["font",
	 * {"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"} ],"ddd " ] }
	 * 
	 * private static Poll2_ResponseBody_Result_Value parseFromJsonStr( String
	 * msgStr) throws Exception { // TODO Auto-generated method stub if
	 * (StringUtils.isBlank(msgStr)) return null;
	 * 
	 * JsonFactory f = new JsonFactory(); JsonParser jp =
	 * f.createJsonParser(msgStr); Poll2_ResponseBody_Result_Value user = new
	 * Poll2_ResponseBody_Result_Value(); jp.nextToken(); // will return
	 * JsonToken.START_OBJECT (verify?) while (jp.nextToken() !=
	 * JsonToken.END_OBJECT) { String fieldname = jp.getCurrentName();
	 * jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
	 * 
	 * if ("content".equals(fieldname)) { // contains an object //
	 * content的内容只能通过手工解析了 String contentStr = jp.getText();
	 * parseContent(contentStr, user);
	 *  } else if ("msg_id".equals(fieldname)) {
	 * user.setMsg_id(jp.getIntValue());
	 *  } else if ("from_uin".equals(fieldname)) {
	 * user.setFrom_uin(jp.getLongValue());
	 *  } else if ("to_uin".equals(fieldname)) {
	 * user.setTo_uin(jp.getLongValue());
	 *  } else if ("msg_id2".equals(fieldname)) {
	 * user.setMsg_id2(jp.getIntValue()); } else if
	 * ("msg_type".equals(fieldname)) { user.setMsg_type(jp.getIntValue()); }
	 * else if ("reply_ip".equals(fieldname)) {
	 * user.setReply_ip(jp.getLongValue()); } else if ("time".equals(fieldname)) {
	 * user.setTime(jp.getLongValue()); } else { throw new
	 * IllegalStateException("Unrecognized field '" + fieldname); } }
	 * jp.close(); // ensure resources get cleaned up timely and properly
	 * 
	 * return user; }
	 */

	/**
	 * 
	 * @param contentStr
	 *            [["font",{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}],"adfaf "] [
	 *            ["font",
	 *            {"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}
	 *            ],"ddd " ]
	 * @param user
	 * 
	 * public static void parseContent(String contentStr,
	 * Poll2_ResponseBody_Result_Value user) { if
	 * (StringUtils.isBlank(contentStr)) return; // 先获取font int idx =
	 * contentStr.indexOf("{\"size\""); int idx2 = contentStr.indexOf("}]");
	 * String fontStr = contentStr.substring(idx, idx2 + 1); idx =
	 * contentStr.lastIndexOf("\"]"); String msgStr = contentStr.substring(idx2 +
	 * 4, idx); user.setContentMsg(msgStr);
	 * 
	 * ObjectMapper map = new ObjectMapper(); try { ContentFont font =
	 * map.readValue(fontStr, ContentFont.class); user.setFont(font);
	 *  } catch (JsonParseException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } catch (JsonMappingException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); } catch (IOException e) { //
	 * TODO Auto-generated catch block e.printStackTrace(); } }
	 */

	public static String Key_Msg_id = "msg_id";

	public Poll2_ResponseBody_Result_Value()
	{
		
	}
	public Poll2_ResponseBody_Result_Value(PollResult pollObj) {
		if(pollObj == null)
			return;
		msg_id = pollObj.getMsg_id();
		setMsg_id(msg_id);
		
		from_uin = pollObj.getFrom_uin();
		setFrom_uin(from_uin);
		
		to_uin = pollObj.getTo_uin();
		setTo_uin(to_uin);
		
		msg_id2 = pollObj.getMsg_id2();
		setMsg_id2(msg_id2);
		
		msg_type = pollObj.getMsg_type();
		setMsg_type(msg_type);
		
		reply_ip = pollObj.getReply_ip();
		setReply_ip(reply_ip);
		
		time = pollObj.getTime();
		setTime(time);
		
		if(StringUtils.isNotBlank(pollObj.getFont()))
			try {
				font = Jackson.getObjectMaper().readValue(pollObj.getFont(), ContentFont.class);
				setFont(font);
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		contentMsg = pollObj.getContentMsg();
		setContentMsg(contentMsg);
			
		mode = pollObj.getMode();
		setMode(mode);
		
		type = pollObj.getType();
		setType(type);
		
		name = pollObj.getName();
		setName(name);
		
		session_id = pollObj.getSession_id();
		setSession_id(session_id);
		
		inet_ip = pollObj.getInet_ip();
		setInet_ip(inet_ip);
		
		cancel_type = pollObj.getCancel_type();
		setCancel_type(cancel_type);
		
		lc_id = pollObj.getLc_id();
		setLc_id(lc_id);
	}

	public long getMsg_id() {
		long msg_id = (Long) getLong(Key_Msg_id);
		return msg_id;
	}

	public void setMsg_id(long msg_id) {
		super.put(Key_Msg_id, msg_id);
	}

	public static final String Key_From_uin = "from_uin";

	public long getFrom_uin() {
		return getLong(Key_From_uin);
	}
	
	public long getLong(String key) {
		Object o = get(key);
		if(o instanceof Integer) {
			int uinInt = (Integer)o;
			return uinInt;
		}
		
		if(o == null)
			return 0;
		long from_uin = (Long) o;
		return from_uin;
	}
	
	public int getInt(String key) {
		Object o = get(key);
		if(o instanceof Integer) {
			int uinInt = (Integer)o;
			return uinInt;
		}
		if(o == null)
			return 0;
		return (Integer)o;
	}

	public void setFrom_uin(long from_uin) {
		super.put(Key_From_uin, (Long)from_uin);
	}

	public static final String Key_To_uin = "to_uin";

	public long getTo_uin() {
		return getLong(Key_To_uin);
	}

	public void setTo_uin(long to_uin) {
		super.put(Key_To_uin, to_uin);
	}

	public static final String Key_Msg_id2 = "msg_id2";

	public long getMsg_id2() {
		long msg_id2 = (Long) getLong(Key_Msg_id2);
		return msg_id2;
	}

	public void setMsg_id2(long msg_id2) {
		put(Key_Msg_id2, msg_id2);
	}

	public static final String Key_Msg_type = "msg_type";

	public int getMsg_type() {
		int msg_type = (Integer) getInt(Key_Msg_type);
		return msg_type;
	}

	public void setMsg_type(int msg_type) {
		put(Key_Msg_type, msg_type);
	}

	public static final String Key_Reply_ip = "reply_ip";

	public long getReply_ip() {
		long reply_ip = (Long) getLong(Key_Reply_ip);
		return reply_ip;
	}

	public void setReply_ip(long reply_ip) {
		put(Key_Reply_ip, reply_ip);
	}

	public static final String Key_Time = "time";

	public long getTime() {
		long time = (Long) getLong(Key_Time);
		return time;
	}

	public void setTime(long time) {
		put(Key_Time, time);
	}


	public String getContentMsg() {
		if(StringUtils.isNotBlank(this.contentMsg))
			return contentMsg;
		
		String contentMsg = (String) filterContent(null);
		return contentMsg;
	}

	public void setContentMsg(String contentMsg) {
		this.contentMsg = contentMsg;
		//获取content对应的对象
		List contents = (List) get(Key_Content);

		//如果为空则生成一个新的
		if(contents == null) {
			contents = new ArrayList<Object>();
			put(Key_Content, contents);
		}
		
		//设置上内容对象 这里应该是第二个位置上 这里先不做处理 调用的时候记住 最后设置设个属性就好了
		contents.add(contentMsg);
	}

	/**
	 * "content":[ ["font",
	 * {"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"} ],
	 * "ddd " ] }
	 * 
	 * @return
	 */

	public static String Key_Font = "font";
	public static String Key_Font_size = "size";
	public static String Key_Font_color = "color";
	public static String Key_Font_style = "style";
	public static String Key_Font_name = "name";

	public String getFontStr() {
		ContentFont font = getFont();
		if(font != null) {
			try {
				return Jackson.getObjectMaper().writeValueAsString(font);
			} catch (JsonGenerationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//默认的
		return "{\"size\":13,\"color\":\"000000\",\"style\":[0,0,0],\"name\":\"\u9ED1\u4F53\"}";
	}
	public ContentFont getFont() {
		if(this.font != null)
			return font;
		
		List fontList = (List) filterContent(Key_Font);
		ContentFont font = null;
		if (fontList != null && fontList.size() > 1) {
			Map fontInfo = (Map) fontList.get(1);
			font = new ContentFont();
			font.setColor((String) fontInfo.get(Key_Font_color));
			font.setName((String) fontInfo.get(Key_Font_name));
			font.setSize(String.valueOf(fontInfo.get(Key_Font_size)));

			List<Integer> styleList = (List) fontInfo.get(Key_Font_style);
			if (styleList != null && !styleList.isEmpty()) {

				int[] style = new int[styleList.size()];
				int i = 0;
				for (Integer styleInt : styleList) {
					style[i] = styleList.get(i++);
				}
				font.setStyle(style);
			}
		}
		return font;
	}
	
	public void setFont(String font2) {
		try {
			ContentFont fontObj = Jackson.getObjectMaper().readValue(font2, ContentFont.class);
			this.setFont(fontObj);
			return;
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//如果有异常则设置一个默认的吧
		//{\"size\":13,\"color\":\"000000\",\"style\":[0,0,0],\"name\":\"\u9ED1\u4F53\"}
		ContentFont fontObj = new ContentFont();
		fontObj.setSize("13");
		fontObj.setColor("000000");
		fontObj.setStyle(new int[]{0,0,0});
		fontObj.setName("宋体");
		setFont(fontObj);
	}
	
	
	/**
	 * 该方法不能重复调用 否则将造成有多个font信息
	 * @param fontObj
	 */
	private void setFont(ContentFont fontObj) {
		this.font = fontObj;
		
		//设置到map里面
		List<Object> contents = (List)get(Key_Content);
		if(contents == null) {
			contents = new ArrayList<Object>();
			put(Key_Content, contents);
		}
		
/**
 * 	 * "content":[ ["font",
	 * {"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"} ],
	 * "ddd " ] }
 */
		List<Object> fonList = new ArrayList<Object>();
		fonList.add(Key_Font);
		try {
			fonList.add(fontObj);
		} catch (Exception e) {
			e.printStackTrace();
			//有异常则设置一个默认的
			fonList.add(fontObj);
		} 
		contents.add(fonList);
	}

	public static String Key_Content = "content";

	/**
	 * 获取 key对应的content内容
	 * 
	 * @param key
	 * @return List of ["font",
	 *         		{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"} 
	 *         ]
	 *         OR if key == null
	 *         return content中的单个字符串
	 */
	public static final String Key_Content_cface = "cface";
	public Object filterContent(String key) {
		List contents = (List) get(Key_Content);
		
		if (contents != null && !contents.isEmpty()) {
			List retList = new ArrayList<List>();//返回的结果
			
			Object conObject;
			List contentList;
			
			for (int i = 0; i < contents.size(); i++) {
				conObject = contents.get(i);
				if (key != null && conObject instanceof List) {
					contentList = (List) conObject;
					if (!contentList.isEmpty()
							&& key.equals(contentList.get(0))) {
						retList.add(contentList);
					}
				} else if (key == null && conObject instanceof String) {
					// 为空时获取单个字符串元素
					return conObject;
				}
			}
			
			return retList;
		}

		return null;
	}

	public static String Key_Mode = "mode";
	public void setMode(String mode) {
		put(Key_Mode, mode);
	}

	public static String Key_Type = "type";
	public void setType(int type) {
		put(Key_Type, type);
	}

	public static String key_Name = "name";
	public void setName(String name) {
		put(key_Name, name);
	}

	public static String Key_Session_id =  "session_id";
	public void setSession_id(long session_id) {
		put(Key_Session_id, session_id);
	}

	public static String Key_Inet_ip = "inet_ip";
	public void setInet_ip(long inet_ip) {
		put(Key_Inet_ip, inet_ip);
	}

	public String getMode() {
		String mode = (String)get(Key_Mode);
		return mode;
	}

	public int getType() {
		int type = (Integer)getInt(Key_Type);
		return type;
	}

	public String getName() {
		String name = (String)get(key_Name);
		return name;
	}

	public long getSession_id() {
		long session_id = (Long)getLong(Key_Session_id);
		return session_id;
	}

	public long getInet_ip() {
		long inet_ip = (Long)getLong(Key_Inet_ip);
		return inet_ip;
	}

	public static String Key_Cancel_type = "cancel_type";
	public void setCancel_type(int cancel_type) {
		
		put(Key_Cancel_type, cancel_type);

	}

	public int getCancel_type() {
		int cancel_type = (Integer)getInt(Key_Cancel_type);
		return cancel_type;
	}

	public static String key_Lc_id = "lc_id";
	public int getLc_id() {
		return (Integer)getInt(key_Lc_id);
	}

	public void setLc_id(int lc_id) {
		this.lc_id = lc_id;
		put(key_Lc_id, lc_id);
	}
	
	private static String Key_Uin = "uin";
	public long getUin() {
		return getLong(Key_Uin);
	}
	public void setUin(long uin) {
		this.uin = uin;
		put(Key_Uin, uin);
	}
	
	private static String Key_Status = "status"; 
	public String getStatus() {
		return (String)get(Key_Status);
	}
	public void setStatus(String status) {
		this.status = status;
		put(Key_Status, status);
	}
	
	private static String Key_ClientType = "client_type";
	public int getClient_type() {
		return getInt(Key_ClientType);
	}
	public void setClient_type(int client_type) {
		this.client_type = client_type;
		put(Key_ClientType, client_type);
	}


}
